//
// Created by Administrator on 2024-1-11.
//

#include <iostream>
#include <bitset>
#include <valarray>

void _and(int x, int y);

void _quFang(int i, int i1);

using namespace std;

/*
 * std::cout
 */
int main1() {
//    \n == endl

    cout << "hello \n";
    cout << "2 \t 3 \\ \"" << endl;
    return 0;
}

/*
 * Variables
 *  int  123 -123
 *  double 19.99
 *
 *  char 'a' 'B'
 *  string(小写）  "hello"
 *
 *   bool true false
 * declare
 *   type name = value;
 *   name
 *      begin with a letter or an underscore (_)
 *      (myVar and myvar are different variables)
 *
 */
int mainV() {

    int a = 1;
    int b;
    b = 2;

    double c = 1.1;

    char d = 'd';
//    https://www.w3schools.com/charsets/ref_html_ascii.asp
    char d2 = 65;

    string e = "eee";

//    f false == 0 true == 1
    bool f = false;

    cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" << f << endl;
    cout << d2 << endl;
    return 0;
}

/*
 * constants
 */
int main3() {
    const int myNum = 15;
//    error  unchangeable and read-only
//    myNum = 10;
    return 0;
}

/*
 * input cin
 */
//int main() {
int mainCin() {
    int x;
    cin >> x;
    cout << x << endl;;

//    cin considers a space (whitespace, tabs, etc) as a terminating character,
//  getline()
// input "j d"
    string name;
    cin >> name;
    cout << name << endl;

    string name2;
    getline(cin, name2);
    cout << name2 << endl;
    return 0;
}

/*
 *  basic data types
 *      1 byte: boolean,char
 *      2 or 4 bytes : int
 *      4 bytes : float
 *          only six or seven decimal digits
 *      8 bytes : double
 *         a precision of about 15 digits
 *
 *  float vs double： 小数点后有多少位, double 更safer
 *
 *  "e" : the power of 10 ,10的幂
 */
int mainBasicDataTypes() {
//    35000
    float f1 = 35e3;
//    120000
    double d1 = 12e4;
    double d2 = 12E4;
    cout << f1 << endl;
    cout << d1 << endl;
    cout << d2 << endl;

    return 0;
}


/*
 *  operators
 *
 *  1. Arithmetic : + - * / % ++ --
 *  2. Assignment ：
 *      = += -= *= /= %=
 *      &= |= ^= >>= <<=
 *   位运算符
 *      1. 速度快
 *      2. 一个位表示
 *      如俄罗斯方块上点 （显示或者不显示），用char[4][4]又慢，内存差点暴了
 *
 *      交换两个数字 更快
 *      a ^= b; b ^= a; a ^= b;
 *
 *
 */
//int main() {
int mainOperator() {
    /*
     * & 与:  两个都是1           才为1
     * | 或:  两个有一个1         就为1
     * ^ 异或：两个不同（异）      就为1
     *
        101
        011
        1

        010
        011
        2
     */
//    _and(5, 3);// 001
//    _and(2, 3);// 010

    /*
     * ~ 取反：  0变1  1变0
     * << 左移： 高位丢弃,低位补0
     * >> 右移： 对无符号数，高位补 0，对于有符号数，高位补符号位
     */
//    _quFang(3, 3);
//    _quFang(5, 3);

    int x = 5;
    bitset<32> binaryX(x);
    cout << binaryX << endl;
    x >>= 3;
    cout << x << endl;

    x = 5;
    cout << binaryX << endl;
    x <<= 3;
    bitset<32> binaryAX(x);
    cout << binaryAX << endl;
    cout << x << endl;


    return 0;
}


void _quFang(int x, int y) {
    bitset<32> binaryX(x);
    bitset<32> binaryY(y);
    int c = x ^ y;


    cout << binaryX << endl;
    cout << binaryY << endl;
    cout << c << endl;
}

void _and(int x, int y) {
    bitset<32> binaryX(x);
    bitset<32> binaryY(y);
    x &= y;

    cout << binaryX << endl;
    cout << binaryY << endl;
    cout << x << endl;

}

/*
 * string
 */
//int main() {
int mainString() {
    string first = "John";
    string last = "Doe";
    string fullName = first + last;
    cout << fullName << endl;

    string full2 = first.append(last);
    cout << full2 << endl;

    cout << full2.length() << full2.size() << endl;

    cout << full2[0] << endl;
    full2[1] = 'w';
    cout << full2 << endl;
    return 0;

}

/*
 * namespace
 */
int mainName() {
//    using namespace std
    std::string greeting = "hello";
    std::cout << greeting << std::endl;
    return 0;
}

int mainMath() {
    cout << std::max(5, 10) << endl;
    cout << std::min(5, 10) << endl;

    cout << sqrt(64) << endl;
    cout << round(64) << endl;
    cout << log(64) << endl;

    return 0;
}

/*
 * array
 *
 */
int mainArray() {
    string cars[4] = {"BMW", "FORD", "Mazda", "Volvo"};
    int nums[4] = {1, 2, 3,};

    // show 16 instead of 4, int type is usually 4 bytes, 4byte * 4 =16bytes
    cout << sizeof(nums) << endl;
    cout << sizeof(nums) / sizeof(int) << endl;
//    cout << nums.size() << endl;  //error

    cout << sizeof(cars) << endl;
    cout << sizeof(string) << endl;
    cout << sizeof(cars) / sizeof(string) << endl;

    return 0;
}


int mainStruct() {
    struct {
        int myNum{};
        string myString;
    } myStruct, myStruct2;

    myStruct.myNum = 1;
    myStruct.myString = "str1";

    cout << myStruct.myNum << endl;
    cout << myStruct.myString << endl;

    struct car {
        string brand;
        string model;
    };
    car car1;
    car1.brand = "brand1";
    car1.model = "model1";
    cout << car1.brand << endl;
    cout << car1.model << endl;

    return 0;
}

int mainPtr() {
    string food = "pizza";
    string &meal = food;

    cout << food << endl;
    cout << meal << endl;
    cout << &meal << endl;
    cout << &food << endl;


//    string* ptr = food; //error
    string *ptr = &food;

    cout << "ptr" << endl;
    cout << ptr << endl;
    cout << &ptr << endl;
    cout << *ptr << endl;

//    ptr = "1";//error
    *ptr = "1";
    cout << " *ptr update " << endl;
    cout << ptr << endl;
    cout << &ptr << endl;
    cout << *ptr << endl;
    cout << food << endl;

}